package com.rayo.accesscontrollercopy.model.service;


import com.rayo.accesscontrollercopy.config.GlobalVariables;
import com.rayo.accesscontrollercopy.config.OfficeModeConfig;
import com.rayo.accesscontrollercopy.model.db.bean.DmuTimeData;
import com.rayo.accesscontrollercopy.utils.TimeFormatUtil;
import com.rayo.wallreader.gpio.OnRelayListener;
import com.rayo.wallreader.gpio.RelayBiz;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by yanghaojie on 2017/3/15.
 */

public class OfficeModeThread implements Runnable {
    private static RelayBiz mRelayBiz = new RelayBiz(RelayBiz.RELAY1);
    private static boolean isOpen;
    private static boolean isRunning;

    @Override
    public void run() {
        mRelayBiz.closeRelay(sOnOpenListener);
        isRunning = true;
        while (isRunning) {
            officeMode();
            try {
                Thread.sleep(60 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static OnRelayListener.OnOpenListener sOnOpenListener = new OnRelayListener.OnOpenListener() {
        @Override
        public void openSuccess() {
            isOpen = true;
        }

        @Override
        public void openFailed() {
            isOpen = false;
        }

        @Override
        public void closeSuccess() {
            isOpen = false;
        }

        @Override
        public void closeFailed() {
            isOpen = false;
        }
    };

    public static void officeMode() {
        if (OfficeModeConfig.OFFICE_MODE == OfficeModeConfig.getInstance().getOfficeMode()) {
            if (isInOfficeTime()) {
                if (!isOpen)
                    mRelayBiz.openRelay(sOnOpenListener);
            } else {
                if (isOpen)
                    mRelayBiz.closeRelay(sOnOpenListener);
            }
        }
    }

    public static void open() {
        switch (OfficeModeConfig.getInstance().getOfficeMode()) {
            case OfficeModeConfig.OFFICE_MODE:
                mRelayBiz.openRelay(sOnOpenListener);
                break;
            case OfficeModeConfig.STANDAR_MODE:
                mRelayBiz.openRelaySec(GlobalVariables.getInstance().getmSystemData().getDelay(), sOnOpenListener);
                break;
            case OfficeModeConfig.ON_OFF_MODE:
                mRelayBiz.onOffRelay(sOnOpenListener);
                break;
            case OfficeModeConfig.TIME_ON_OFF_MODE:
                if (isInOfficeTime()) {
                    mRelayBiz.onOffRelay(sOnOpenListener);
                } else {
                    mRelayBiz.openRelaySec(GlobalVariables.getInstance().getmSystemData().getDelay(), sOnOpenListener);
                }
                break;
            default:
                break;
        }
    }

    public static void remoteOpen() {
        mRelayBiz.openRelaySec(GlobalVariables.getInstance().getmSystemData().getDelay(), sOnOpenListener);
    }

    private static final String formatStr = "HH:mm";
    private static SimpleDateFormat sdf = new SimpleDateFormat(formatStr);

    private static boolean isInZone(long tStart, long tEnd, long t) throws ParseException {
        return tStart <= t && t <= tEnd;
    }

    private static long getLong(String timeStr) throws ParseException {
        return sdf.parse(timeStr).getTime();
    }

    private static long getCurrentTime() throws ParseException {
        return getLong(sdf.format(new Date()));
    }

    private static boolean isInOfficeTime() {
        Calendar now = Calendar.getInstance();
        boolean isFirstSunday = (now.getFirstDayOfWeek() == Calendar.SUNDAY);
        int weekDay = now.get(Calendar.DAY_OF_WEEK);
        if (isFirstSunday) {
            weekDay = weekDay - 1;
            if (weekDay == 0) weekDay = 7;
        }
        try {
            for (DmuTimeData dmuTimeData : OfficeModeConfig.getInstance().getDmuTimeDatas()) {
                String tS = TimeFormatUtil.dateToTime(dmuTimeData.getFrom());
                String tE = TimeFormatUtil.dateToTime(dmuTimeData.getTo());
                if (isInZone(getLong(tS), getLong(tE), getCurrentTime())) {
                    switch (weekDay) {
                        case 1:
                            if (dmuTimeData.getMon() == 1)
                                return true;
                        case 2:
                            if (dmuTimeData.getTue() == 1)
                                return true;
                        case 3:
                            if (dmuTimeData.getWed() == 1)
                                return true;
                        case 4:
                            if (dmuTimeData.getThu() == 1)
                                return true;
                        case 5:
                            if (dmuTimeData.getFri() == 1)
                                return true;
                        case 6:
                            if (dmuTimeData.getSat() == 1)
                                return true;
                        case 7:
                            if (dmuTimeData.getSun() == 1)
                                return true;
                        default:
                            break;
                    }
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}
